package com.gmcc.docmanager.loader;

import java.io.IOException;
import java.io.InputStream;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.apache.http.HttpResponse;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import com.gmcc.docmanager.app.Constants;
import com.gmcc.docmanager.util.ErrorCodeUtils;
import com.gmcc.docmanager.util.loader.UrlLoaderParser;

public abstract class ResponseParser extends UrlLoaderParser {

	protected ResponseListener listener;
	protected boolean netResult;
	protected Element rootElement;

	public ResponseParser(ResponseListener listener) {
		this.listener = listener;
	}

	@Override
	public void doParse(String url, HttpResponse httpresp, InputStream is,
			String postData) {
		netResult = true;
		if ((httpresp == null && !Constants.IS_INTERFACE_TEST) || is == null) {
			response(createFailureResponse(100));
			netResult = false;
		} else if (httpresp != null
				&& httpresp.getStatusLine().getStatusCode() != 200) {
			response(createFailureResponse(httpresp.getStatusLine()
					.getStatusCode()));
			netResult = false;
		} else {
			try {
				DocumentBuilderFactory factory = DocumentBuilderFactory
						.newInstance();
				DocumentBuilder db = factory.newDocumentBuilder();
				Document document = db.parse(is);
				rootElement = document.getDocumentElement();
			} catch (Exception e) {
				netResult = false;
				listener.onResponse(createFailureResponse(99));
				if (is != null) {
					try {
						is.close();
					} catch (IOException e1) {
						e1.printStackTrace();
					}
				}
			}
		}

	}

	protected Response createFailureResponse(String errorMsg) {
		Response response = new Response();
		response.setCode(-1);
		response.setDescription(errorMsg);
		return response;
	}

	protected Response createFailureResponse(String tag, int code) {
		Response response = new Response();
		response.setCode(code);
		response.setDescription(ErrorCodeUtils.getErrorDescription(tag,
				String.valueOf(code)));
		return response;
	}

	protected Response createFailureResponse(int code) {
		return createFailureResponse("common", code);
	}

	protected void response(Response response) {
		if (listener != null && response != null) {
			listener.onResponse(response);
		}
	}

	protected String getAttributeValue(NamedNodeMap nodeMap, String name) {
		if (nodeMap != null && nodeMap.getLength() > 0) {
			Node valueNode = nodeMap.getNamedItem(name);
			if (valueNode != null) {
				return valueNode.getNodeValue();
			}
		}
		return null;
	}

	protected Node getSingleNode(String nodeName) {
		NodeList nodeList = rootElement.getElementsByTagName(nodeName);
		if (nodeList != null && nodeList.getLength() > 0) {
			return nodeList.item(0);
		}
		return null;
	}

	protected String getNodeTextValue(String nodeName) {
		NodeList nodeList = rootElement.getElementsByTagName(nodeName);
		if (nodeList != null && nodeList.getLength() > 0) {
			Node node = nodeList.item(0);
			return getNodeTextValue(node);
		}
		return null;
	}

	protected String getNodeTextValue(Node node) {
		Node valueNode = node.getFirstChild();
		if (valueNode != null) {
			return valueNode.getNodeValue();
		}
		return null;
	}

	protected boolean stringToBoolean(String value) {
		try {
			return Boolean.valueOf(value);
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}

	protected int stringToInt(String value) {
		try {
			return Integer.valueOf(value);
		} catch (Exception e) {
			e.printStackTrace();
			return -1;
		}
	}
}
